package com.fau.om.aop;

import javax.annotation.Resource;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;

import com.fau.om.dao.IBaseDao;
import com.fau.om.entity.MedicalAdmin;
import com.fau.om.entity.MedicalNuclearCheck;
import com.fau.om.entity.MedicalOrders;
import com.fau.om.util.SystemConfig;

/**
 * @desc 员工绩效考核Aop
 * @author dangjh
 * @datetime 2014年12月11日 下午4:45:18
 */
@Aspect
@Component
public class PerformAppraisalAop {

	@Resource
	private IBaseDao iBaseDao;
	
	/**
	 * @desc 分配切点
	 * @author dangjh
	 * @datetime 2014年12月11日 下午9:21:46
	 */
	@Pointcut("execution(* com.fau.om.service.impl.AllocationManagerService.allocateUser(..))")
	private void allocateUser() {}
	
	/**
	 * @desc 审核切点
	 * @author dangjh
	 * @datetime 2014年12月11日 下午9:22:04
	 */
	@Pointcut("execution(* com.fau.om.service.impl.AuditManagerService.auditOrdersPass(..))")
	private void auditOrdersPass() {}
	
	/**
	 * @desc 订单无效切点
	 * @author dangjh
	 * @datetime 2014年12月11日 下午9:22:04
	 */
	@Pointcut("execution(* com.fau.om.service.impl.AuditManagerService.auditOrdersInvalid(..))")
	private void auditOrdersInvalid() {}
	
	/**
	 * @desc 保存核单员绩效考核数据
	 * @author dangjh
	 * @datetime 2014年12月11日 下午9:10:18
	 */
	@AfterReturning("allocateUser()")
	public void allocate(JoinPoint joinPoint) {
		Object[] obj = joinPoint.getArgs();
		String orderIds = (String) obj[0];
		String[] ids = orderIds.split(",");
		for(String oId : ids) {
			MedicalOrders order = (MedicalOrders) this.iBaseDao.getObject(MedicalOrders.class, oId);
			if(null != order) {
				MedicalNuclearCheck	nuclearCheck = new MedicalNuclearCheck();
				MedicalAdmin user = (MedicalAdmin) this.iBaseDao.getObjectByHql("from MedicalAdmin m where m.id = ? ", order.getCheckUserId());
				if(!SystemConfig.SUPER_ADMIN.equals(user.getIsSuper())) {
					nuclearCheck.setUser(user);
					nuclearCheck.setOrderId(order.getId());
					nuclearCheck.setStatus(SystemConfig.ORDERS_WAIT_AUDIT_TYPE);
					nuclearCheck.setEid(order.getHospitalId());
					this.iBaseDao.saveObject(nuclearCheck);
				}
			}
		}
	}
	
	/**
	 * @desc 审核通过
	 * @author dangjh
	 * @datetime 2014年12月11日 下午4:47:20
	 */
	@AfterReturning("auditOrdersPass()")
	public void audiOrders(JoinPoint joinPoint) {
		Object[] obj = joinPoint.getArgs();
		MedicalOrders orders = (MedicalOrders) obj[1];
		orders = (MedicalOrders) this.iBaseDao.getObjectByHql("from MedicalOrders m where m.id = ? ", orders.getId());
		// 当前用户
		MedicalAdmin user = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		if(SystemConfig.OM_SYSTEM_TYPE.equals(user.getDeptType())) {
			// 核单员绩效考核
			MedicalNuclearCheck	nuclearCheck = (MedicalNuclearCheck) this.iBaseDao.getObjectByHql("from MedicalNuclearCheck m where m.orderId = ? ", orders.getId());
			// 此种情况为未分配给核单员，核单员直接进行操作的情况
			if(null == nuclearCheck) {
				nuclearCheck = new MedicalNuclearCheck();
				MedicalAdmin user1 = new MedicalAdmin();
				user1.setId(user.getId());
				nuclearCheck.setUser(user1);
				nuclearCheck.setOrderId(orders.getId());
				nuclearCheck.setEid(orders.getHospitalId());
			}
			nuclearCheck.setStatus(SystemConfig.ORDERS_AUDIT_PASS_TYPE);
			this.iBaseDao.saveObject(nuclearCheck);
		}
	}
	
	/**
	 * @desc 订单无效通过
	 * @author dangjh
	 * @datetime 2014年12月11日 下午4:47:20
	 */
	@AfterReturning("auditOrdersInvalid()")
	public void auditOrdersInvalid(JoinPoint joinPoint) {
		Object[] obj = joinPoint.getArgs();
		MedicalOrders orders = (MedicalOrders) obj[0];
		this.iBaseDao.updateHql("delete from MedicalCallPerforAnalysis m where m.ordersId = ? ", orders.getId());
	}
	
}
